//
//  MyWishViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/4/8.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "MyWishViewController.h"
#import "MyWishTableViewCell.h"
#import "WishListTableViewCell.h"
#import "WishListModle.h"
#import "OrderDetailViewController.h"
#import "VowListViewController.h"
#import "SubmitOrderViewController.h"
@interface MyWishViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *ownArray;
@property (nonatomic, strong)WishListModle *model;
@property (nonatomic, assign) int pageNo;

@property (nonatomic, assign) NSString* isPublic;
@property (nonatomic, assign) BOOL isMatch;

@property (nonatomic, strong) NSString * prayId;

@end

@implementation MyWishViewController
-(void)viewWillDisappear:(BOOL)animated{
    self.shopModle=nil;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.shopModle!=nil) {
        self.isPublic=nil;
        NSString*msg;
        if (self.shopModle.isMatch==YES) {
            msg=[NSString stringWithFormat:@"是否加持已请的%@",self.shopModle.title];
        }else{
            msg=[NSString stringWithFormat:@"您还未请供该佛像,确认后将自动提交订单,支付完成系统会自动提交您的心愿,是否确认!"];
        }
        LXAlertView*alert=[[LXAlertView alloc]initWithTitle:nil message:msg cancelBtnTitle:@"取消" otherBtnTitle:@"确定" clickIndexBlock:^(NSInteger clickIndex) {
            if (clickIndex==1) {
                if (self.shopModle.isMatch==YES) {
                    [self loadpublicRequest];
                }else{
                    NSMutableDictionary*dicc=[NSMutableDictionary dictionary];
                    if (self.shopModle.isMatch==YES) {
                        dicc[@"isMatch"]    =@"0";
                    }else{
                        dicc[@"isMatch"]    =@"1";
                    }
                    dicc[@"skuId"]=self.shopModle.skuId;
                    dicc[@"prayId"]=self.prayId;
                    
                    
                    NSMutableDictionary *dic=[[NSMutableDictionary   alloc]init];
                    dic[@"skuId"]=self.shopModle.skuId;
                    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
                    dic[@"number"]=@"1";
                    [self doHttpRequestWithApi:API_SHOPBUYNOW_LIST method:POST dic:dic successBlock:^(NSDictionary *dic) {
                        NSLog(@"%@",dic);
                        SubmitOrderViewController*vc=[[SubmitOrderViewController alloc]init];
                        vc.Praydic=dicc;
                        vc.orderDic=dic[@"data"];
                        vc.styple=Supplication;
                        [self.navigationController pushViewController:vc animated:YES];
                    }];
                }
            }
        }];
        [alert showLXAlertView];
        NSLog(@"曹尼玛");
    }
}
-(NSMutableArray *)ownArray{
    if (!_ownArray) {
        _ownArray=[NSMutableArray array];
    }
    return _ownArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageNo=1;
    self.title=MyString(@"我的心愿");
    [self vowListRequest];
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MainW, MainH-64) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle=0;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    self.tableView.backgroundColor=HJ_Color(240, 240, 240);
    self.tableView.mj_footer=[MJRefreshBackStateFooter footerWithRefreshingBlock:^{
        self.pageNo++;
        [self vowListRequest];
        [self.tableView.mj_footer endRefreshing];
    }];
    [tableView registerNib:[UINib nibWithNibName:@"MyWishTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MyWishTableViewCell"];
    [tableView registerNib:[UINib nibWithNibName:@"WishListTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"WishListTableViewCell"];
    self.view.backgroundColor=HJ_Color(240, 240, 240);
    [self showWishTempView:@"您还没有许愿,快去许个愿吧!" Image:@"image_wish_blank" CGFrame:CGRectMake(0, 0, MainW,MainH-64)];
    self.tempView.hidden=YES;
    // Do any additional setup after loading the view.
}

-(void)createView{
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.ownArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.model=self.ownArray[indexPath.section];
    CGFloat height=[ZJUITools getHeightByWidth:MainW-30 title:[NSString stringWithFormat:@"%@",self.model.prayInfo] font:FontAndStyle12];
    if (self.model.imageUrl==nil){
        if ([self.model.status intValue]==5) {
            return 80+height;
        }
        return 80+height+30;
    }
    return 175+height;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.model=self.ownArray[indexPath.section];
    if (self.model.imageUrl==nil) {
        WishListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"WishListTableViewCell"];
        if (!cell) {
            cell = [tableView dequeueReusableCellWithIdentifier:@"WishListTableViewCell"];
        }
        NSMutableArray*array=[NSMutableArray array];
        for (NSDictionary*dic in self.model.prayInfoattr) {
            [array addObject:dic[@"name"]];
        }
        cell.SketchLabel.text=[array componentsJoinedByString:@" "];
        [cell.headImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.model.imageUrl]]];
        cell.timelabel.text=[self timeWithTimeIntervalString:[NSString stringWithFormat:@"%@",self.model.createTime]];
        cell.nameLabel.text=[NSString stringWithFormat:@"%@",self.model.prayInfo];
        if ([self.model.open intValue]==1){
            [cell.rightBtn setTitle:@"取消公开" forState:0];
        }else{
            [cell.rightBtn setTitle:@"公开心愿" forState:0];
        }
        if ([self.model.status intValue]==0) {
             cell.leftBtn.hidden=NO;
             [cell.leftBtn setTitle:@"去加持" forState:0];
        }else if ([self.model.status intValue]==1){
            cell.leftBtn.hidden=YES;
        }else if ([self.model.status intValue]==2){
             cell.leftBtn.hidden=NO;
            [cell.leftBtn setTitle:@"完成加持" forState:0];
        }else if ([self.model.status intValue]==3){
             cell.leftBtn.hidden=NO;
            [cell.leftBtn setTitle:@"祈愿" forState:0];
        }else if ([self.model.status intValue]==4){
             cell.leftBtn.hidden=NO;
            [cell.leftBtn setTitle:@"完成加持" forState:0];
        }else if ([self.model.status intValue]==5){
            cell.leftBtn.hidden=YES;
            cell.rightBtn.hidden=YES;
        }
        cell.leftBtn.tag=indexPath.section;
        cell.rightBtn.tag=indexPath.section;
        [cell.leftBtn addTarget:self action:@selector(leftBtnClickBtn:) forControlEvents:UIControlEventTouchUpInside];
        [cell.rightBtn addTarget:self action:@selector(rightBtnBtnClickBtn:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }else{
        MyWishTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MyWishTableViewCell"];
        if (!cell) {
            cell = [tableView dequeueReusableCellWithIdentifier:@"MyWishTableViewCell"];
        }
        NSMutableArray*array=[NSMutableArray array];
        for (NSDictionary*dic in self.model.prayInfoattr) {
            [array addObject:dic[@"name"]];
        }
        cell.SketchLabel.text=[array componentsJoinedByString:@" "];
        [cell.headImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.model.imageUrl]]];
        cell.timelabel.text=[self timeWithTimeIntervalString:[NSString stringWithFormat:@"%@",self.model.createTime]];
        cell.nameLabel.text=[NSString stringWithFormat:@"%@",self.model.prayInfo];
        if ([self.model.open intValue]==1){
            [cell.rightButton setTitle:@"取消公开" forState:0];
        }else{
            [cell.rightButton setTitle:@"公开心愿" forState:0];
        }
        if ([self.model.status intValue]==0) {
            cell.leftButton.hidden=NO;
            [cell.leftButton setTitle:@"去加持" forState:0];
        }else if ([self.model.status intValue]==1){
            cell.leftButton.hidden=YES;
        }else if ([self.model.status intValue]==2){
            cell.leftButton.hidden=NO;
            [cell.leftButton setTitle:@"完成加持" forState:0];
            cell.leftButton.hidden=NO;
        }else if ([self.model.status intValue]==3){
            [cell.leftButton setTitle:@"祈愿" forState:0];
            cell.leftButton.hidden=NO;
        }else if ([self.model.status intValue]==4){
            [cell.leftButton setTitle:@"完成加持" forState:0];
            cell.leftButton.hidden=NO;
        }else if ([self.model.status intValue]==5){
            cell.leftButton.hidden=YES;
            cell.rightButton.hidden=YES;
        }
        cell.leftButton.tag=indexPath.section;
        cell.rightButton.tag=indexPath.section;
        [cell.leftButton addTarget:self action:@selector(leftBtnClickBtn:) forControlEvents:UIControlEventTouchUpInside];
        [cell.rightButton addTarget:self action:@selector(rightBtnBtnClickBtn:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
    
}
-(void)leftBtnClickBtn:(UIButton *)sender{
    NSMutableArray *resultArray=[NSMutableArray array];
    self.model=self.ownArray[sender.tag];
    self.prayId=self.model.prayId;
    for (NSDictionary*dic in self.model.prayInfoattr) {
        [resultArray addObject:dic[@"optionId"]];
    }
    if ([sender.titleLabel.text isEqualToString:@"去加持"]) {
        LXAlertView*alert=[[LXAlertView alloc]initWithTitle:nil message:@"请选择加持来源" cancelBtnTitle:@"已请" otherBtnTitle:@"匹配" clickIndexBlock:^(NSInteger clickIndex) {
            if (clickIndex==0) {
                VowListViewController*vc=[[VowListViewController alloc]init];
                vc.isMatch=YES;
                self.isMatch=YES;
                vc.popWhere=self.popWhere;
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                VowListViewController*vc=[[VowListViewController alloc]init];
                vc.prayString=[resultArray mj_JSONString];
                vc.isMatch=NO;
                self.isMatch=YES;
                vc.popWhere=self.popWhere;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }];
        alert.closeBtn.hidden=NO;
        [alert showLXAlertView];
    }else if ([sender.titleLabel.text isEqualToString:@"完成加持"]){
        NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
        dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
        dic[@"orderSn"]=self.model.orderSn;
        [self doHttpRequestWithApi:API_ORDERDETAIL_LIST method:GET dic:dic successBlock:^(NSDictionary *dic) {
            OrderDetailViewController*vc=[[OrderDetailViewController alloc]init];
            vc.orderSn=dic[@"data"][@"orderSn"];
            [self.navigationController pushViewController:vc animated:YES];
        }];
    }else if ([sender.titleLabel.text isEqualToString:@"祈愿"]){
        
    }
}

-(void)rightBtnBtnClickBtn:(UIButton *)sender{
    self.model=self.ownArray[sender.tag];
    self.prayId=self.model.prayId;
    NSString*msg;
    if ([sender.titleLabel.text isEqualToString:@"取消公开"]) {
        //取消公开
        self.isPublic=@"0";
        msg=@"是否取消公开心愿,取消后仅自己可见!";
    }else{
        self.isPublic=@"1";
        msg=@"是否公开心愿,公开后所有人可见!";
    }
    LXAlertView*alert=[[LXAlertView alloc]initWithTitle:nil message:msg cancelBtnTitle:@"取消" otherBtnTitle:@"确定" clickIndexBlock:^(NSInteger clickIndex) {
        if (clickIndex==1) {
            [self loadpublicRequest];
        }
    }];
    [alert showLXAlertView];
}
-(void)loadpublicRequest{
    NSMutableDictionary*dic=[NSMutableDictionary dictionary];
    if (self.isMatch==YES) {
        dic[@"isMatch"]    =@"0";
    }else{
        dic[@"isMatch"]    =@"1";
    }
    dic[@"skuId"]=self.shopModle.skuId;
    dic[@"prayId"]=self.prayId;
    dic[@"isPublic"]=self.isPublic;
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    [self doHttpRequestWithApi:userpraychange method:GET dic:dic successBlock:^(NSDictionary *dic) {
        self.pageNo=1;
        [self.ownArray removeAllObjects];
        [self vowListRequest];
    }];
}
-(void)vowListRequest{
    NSMutableDictionary*dic=[NSMutableDictionary dictionary];
    dic[@"pageSize"]=@(10);
    dic[@"pageNumber"]=@(self.pageNo);
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    [self doHttpRequestWithApi:ownerlist method:GET dic:dic successBlock:^(NSDictionary *dic) {
        NSArray *arry=dic[@"data"][@"list"];
        [self.ownArray addObjectsFromArray:[WishListModle mj_objectArrayWithKeyValuesArray:arry]];
        if (self.ownArray.count==0) {
            self.tempView.hidden=NO;
        }else{
            self.tempView.hidden=YES;
            [self.tableView reloadData];
        }
    }];
}
@end
